package com.yanfan.zutai.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.mqtt.manager.MqttRemoteManager;
import com.yanfan.mqtt.model.PushMessageBo;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceUpgrade;
import com.yanfan.zutai.service.IBDeviceUpgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 固件升级Controller
 *
 * @author ruoyi
 * @date 2023-11-06
 */
@RestController
@RequestMapping("/zutai/bDeviceUpgrade")
public class BDeviceUpgradeController extends BaseController {
    @Autowired
    private IBDeviceUpgradeService bDeviceUpgradeService;
    @Resource
    private MqttRemoteManager remoteManager;

    /**
     * 查询固件升级列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BDeviceUpgrade bDeviceUpgrade) {
        startPage();
        List<BDeviceUpgrade> list = bDeviceUpgradeService.selectBDeviceUpgradeList(bDeviceUpgrade);
        return getDataTable(list);
    }

    /**
     * 导出固件升级列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:export')")
    @Log(title = "固件升级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BDeviceUpgrade bDeviceUpgrade) {
        List<BDeviceUpgrade> list = bDeviceUpgradeService.selectBDeviceUpgradeList(bDeviceUpgrade);
        ExcelUtil<BDeviceUpgrade> util = new ExcelUtil<BDeviceUpgrade>(BDeviceUpgrade.class);
        util.exportExcel(response, list, "固件升级数据");
    }

    /**
     * 获取固件升级详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bDeviceUpgradeService.selectBDeviceUpgradeById(id));
    }

    /**
     * 新增固件升级
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:add')")
    @Log(title = "固件升级", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BDeviceUpgrade bDeviceUpgrade) {
        return toAjax(bDeviceUpgradeService.insertBDeviceUpgrade(bDeviceUpgrade));
    }

    /**
     * 修改固件升级
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:edit')")
    @Log(title = "固件升级", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BDeviceUpgrade bDeviceUpgrade) {
        return toAjax(bDeviceUpgradeService.updateBDeviceUpgrade(bDeviceUpgrade));
    }

    /**
     * 删除固件升级
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceUpgrade:remove')")
    @Log(title = "固件升级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bDeviceUpgradeService.deleteBDeviceUpgradeByIds(ids));
    }

    /**
     * 设备固件升级
     */
    @Log(title = "设备固件升级", businessType = BusinessType.INSERT)
    @PostMapping("/deviceUpgrade")
    public AjaxResult deviceUpgrade(@RequestBody BDeviceUpgrade bDeviceUpgrade) {
        String topic = "/IOT/WULIANYUN/DATAUPGRADE/";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("firmwareUrl", bDeviceUpgrade.getHttpApi() + bDeviceUpgrade.getFirmwareUrl());
        for (int i = 0; i < bDeviceUpgrade.getUpgrageImeis().size(); i++) {
            String imei = bDeviceUpgrade.getUpgrageImeis().get(i);
            // 发送至前端
            PushMessageBo messageBo = new PushMessageBo();
            messageBo.setTopic(topic + imei);
            messageBo.setMessage(JSON.toJSONString(jsonObject));
            remoteManager.pushCommon(messageBo);
        }
        return AjaxResult.success();
    }
}
